#include"Configuration.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<nlohmann/json.hpp>
#include<iostream>
#include<fstream>

using std::map;
using std::ifstream;
using std::cout;


Configuration::Configuration(std::string& path)
    :_path(path)
{
    cout<<_path<<" path \n";
}

     Configuration* Configuration::GetInstance()
    {
        return _confi;
    }
    Configuration* Configuration::GetInstance(std::string& path)
{
    if(_confi==nullptr)
    {
        _confi=new Configuration(path);
    }
    return _confi;
}

Configuration*   Configuration::_confi =nullptr;

std::map<std::string,std::string>& Configuration::GetConfigMap()
{
    //读取配置文件
    cout<<_path<<" path \n";
    ifstream ifs(_path.c_str());

    size_t length ;
    ifs.seekg(0,std::ios::end);

    length=ifs.tellg();

    ifs.seekg(0,std::ios::beg);


    char * buf =new char[length+1]{0};

    ifs.read(buf,length);

    nlohmann::json json = nlohmann::json::parse(buf);

    // _map["DICT_PATH"]=json["DICT_PATH"];
    // _map["HMM_PATH"]=json["HMM_PATH"];
    // _map["USER_DICT_PATH"]=json["USER_DICT_PATH"];
    // _map["IDF_PATH"]=json["IDF_PATH"];
    // _map["STOP_WORD_PATH"]=json["STOP_WORD_PATH"];
     for(auto& i: json.items())
     {
        _map.insert({i.key(),i.value()});
     }
    return  _map;
}

Config::Config(Configuration* t)
    :_tmp(t)
{
}
Config::~Config()
{
    if(_tmp)
    {
        delete _tmp;
         _tmp=nullptr;
    }
}

    Configuration* Config::ret()
{
    return _tmp;
}
